import { defineStore } from "pinia";
import { ref, toRefs } from "vue";
const useCodeArrStore = defineStore("codeArr", {
  state: () => ({
    dict: new Array()
  }),
  actions: {
    // 获取字典
    getDict(_key) {
      if (_key == null && _key == "") {
        return null;
      }
      try {
        for (let i = 0; i < this.dict.length; i++) {
          if (this.dict[i].dictType == _key) {
            return this.dict[i].dictDataList;
          }
        }
      } catch (e) {
        return null;
      }
    },
    //根据返回的value值拿数据
    getDictName(_key, codeValue) {
      let returnValue = "";
      try {
        for (let i = 0; i < this.dict.length; i++) {
          if (this.dict[i].dictType == _key) {
            this.dict[i].dictDataList.forEach((item, a) => {
              if (item.dictValue == codeValue) {
                returnValue = item.dictLabel;
              }
            });
            return returnValue;
          }
        }
      } catch (e) {
        return null;
      }
    },
    // 设置字典
    setDict(list) {
      this.dict = list;
    },
    // 删除字典
    removeDict(_key) {
      let bln = false;
      try {
        for (let i = 0; i < this.dict.length; i++) {
          if (this.dict[i].key == _key) {
            this.dict.splice(i, 1);
            return true;
          }
        }
      } catch (e) {
        bln = false;
      }
      return bln;
    },
    // 清空字典
    cleanDict() {
      this.dict = new Array();
    },
    // 初始字典
    initDict() {}
  },
  persist: {
    enabled: true,
    strategies: [{ storage: localStorage }] //指定方式localStorage
  }
});

export default useCodeArrStore;
